package com.hbyj.struts.action;

import gnu.io.SerialPort;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import com.hbyj.db.function.DbTools;
import com.hbyj.gps.SerialReader;
import com.hbyj.hbm.vo.Jbqk01;
import com.hbyj.hbm.vo.Wdwzforryd;
import com.hbyj.hbm.vo.Wdwzforzhdn;
import com.hbyj.hbm.vo.YjdcGpsline;
import com.hbyj.hbm.vo.YjdcGpslineforzhd;
import com.opensymphony.xwork2.ActionSupport;

public class GPSHandleAction extends ActionSupport{
	//定义变量，这个不知道能不能成功啊
	private static SerialReader sr;
	
	public void GetGPSInfo() throws UnsupportedEncodingException, IOException{
		
		//应该是在注册端口什么的，然后new出这个SerialReader关键类
		 HashMap<String, Comparable> params = new HashMap<String, Comparable>();
	     params.put( SerialReader.PARAMS_PORT, "COM5" ); // 端口名称
	     params.put( SerialReader.PARAMS_RATE, 9600 ); // 波特率
	     params.put( SerialReader.PARAMS_TIMEOUT, 1000); // 设备超时时间 1秒
	     params.put( SerialReader.PARAMS_DELAY, 1000 ); // 数据准备端口时间 1秒
	     params.put( SerialReader.PARAMS_DATABITS, SerialPort.DATABITS_8 ); // 数据位
	     params.put( SerialReader.PARAMS_STOPBITS, SerialPort.STOPBITS_1 ); // 停止位
	     params.put( SerialReader.PARAMS_PARITY, SerialPort.PARITY_NONE ); // 无奇偶校验
	     sr = new SerialReader( params );
	     ServletActionContext.getResponse().getOutputStream().write("true".getBytes("utf-8"));
	}
	
	
	public void Getjwd() throws UnsupportedEncodingException, IOException{
		String jwd = sr.GetJWD();
	    //System.out.println("返回的值："+jwd);
		//String jwd = demoGetjwd();
	    ServletActionContext.getResponse().getOutputStream().write(jwd.getBytes("utf-8"));
	}
	
	public String demoGetjwd(){
		String jwdStr = "";
		int jd = (int) (109+Math.ceil((Math.random()*2)));
		int wd = (int)(29+Math.ceil(Math.random()*2));
		int gd = (int)(109+Math.ceil(Math.random()*2));
		jwdStr = Integer.toString(jd)+","+Integer.toString(wd)+","+Integer.toString(gd);
		return jwdStr;
	}
	
	public void insertGpsGuiji() throws UnsupportedEncodingException, IOException{
		HttpServletRequest request = ServletActionContext.getRequest();
		String timeStr = request.getParameter("time");
		String lonlatStr = request.getParameter("lonlatStr");
		String name = request.getParameter("name");
		//对时间做处理
		int year = Integer.valueOf(timeStr.split("年")[0]);
		int month = Integer.valueOf(timeStr.split("年")[1].split("月")[0]);
		int day = Integer.valueOf(timeStr.split("年")[1].split("月")[1].split("日")[0]);
		int hrs = Integer.valueOf(timeStr.split("年")[1].split("月")[1].split("日")[1].split("时")[0]);
		int min = Integer.valueOf(timeStr.split("年")[1].split("月")[1].split("日")[1].split("时")[1].split("分")[0]);
		int sec = Integer.valueOf(timeStr.split("年")[1].split("月")[1].split("日")[1].split("时")[1].split("分")[1].split("秒")[0]);
		Date date = new Date(year-1900, month-1, day, hrs, min, sec);
		YjdcGpsline gpsline = new YjdcGpsline(null, lonlatStr, date, name);
		if(DbTools.saveObj(gpsline)){
			ServletActionContext.getResponse().getOutputStream().write("true".getBytes("utf-8"));
		}else{
			ServletActionContext.getResponse().getOutputStream().write("false".getBytes("utf-8"));
		}
	}
	
	
	public void insertGpsGuijiForZhdn() throws UnsupportedEncodingException, IOException{
		HttpServletRequest request = ServletActionContext.getRequest();
		String timeStr = request.getParameter("time");
		String lonlatStr = request.getParameter("lonlatStr");
		String name = request.getParameter("name");
		String zhdbh = name.split(",")[0];
		String zhdmc = name.split(",")[1];
		String jbqk0102 = name.split(",")[2];
		//对时间做处理
		int year = Integer.valueOf(timeStr.split("年")[0]);
		int month = Integer.valueOf(timeStr.split("年")[1].split("月")[0]);
		int day = Integer.valueOf(timeStr.split("年")[1].split("月")[1].split("日")[0]);
		int hrs = Integer.valueOf(timeStr.split("年")[1].split("月")[1].split("日")[1].split("时")[0]);
		int min = Integer.valueOf(timeStr.split("年")[1].split("月")[1].split("日")[1].split("时")[1].split("分")[0]);
		int sec = Integer.valueOf(timeStr.split("年")[1].split("月")[1].split("日")[1].split("时")[1].split("分")[1].split("秒")[0]);
		Date date = new Date(year-1900, month-1, day, hrs, min, sec);
		YjdcGpslineforzhd gpsline = new YjdcGpslineforzhd(null,lonlatStr,date,zhdbh,zhdmc,jbqk0102);
		if(DbTools.saveObj(gpsline)){
			ServletActionContext.getResponse().getOutputStream().write("true".getBytes("utf-8"));
		}else{
			ServletActionContext.getResponse().getOutputStream().write("false".getBytes("utf-8"));
		}
	}
	
	
	public void queryLishiguiji() throws UnsupportedEncodingException, IOException{
		HttpServletRequest request = ServletActionContext.getRequest();
		String startTime = request.getParameter("startTime");
		String endTime = request.getParameter("endTime");
		String name = request.getParameter("name");
		String hql = "from YjdcGpsline";
		if(!("".equals(startTime)&&"".equals(endTime)&&"".equals(name))){
			hql = "from YjdcGpsline where ";
			if(!"".equals(startTime)){
				hql = hql +"time >= '"+startTime+"' AND ";
			}
			if(!"".equals(endTime)){
				hql = hql +"time <= '"+endTime+"' AND ";
			}
			if(!"".equals(name)){
				hql = hql +"name like '%"+name+"%' AND ";
			}
			hql = hql.substring(0, hql.length()-4);
		}
		List<YjdcGpsline> list = DbTools.findByHql(hql);
		if(list.size()>0){
			JSONArray jsonArray = new JSONArray();
			for(int i = 0;i<list.size();i++){
				YjdcGpsline gpsline = list.get(i);
				jsonArray.add(gpsline);
			}
			ServletActionContext.getResponse().getOutputStream().write(jsonArray.toString().getBytes("utf-8"));
		}
	}
	
	
	public void queryRyd() throws UnsupportedEncodingException, IOException{
		HttpServletRequest request = ServletActionContext.getRequest();
		String startTime = request.getParameter("startTime");
		String endTime = request.getParameter("endTime");
		String hql = "from Wdwzforryd";
		if(!("".equals(startTime)&&"".equals(endTime))){
			hql = "from Wdwzforryd where ";
			if(!"".equals(startTime)){
				hql = hql +"dwsj >= '"+startTime+"' AND ";
			}
			if(!"".equals(endTime)){
				hql = hql +"dwsj <= '"+endTime+"' AND ";
			}
			hql = hql.substring(0, hql.length()-4);
		}
		List list = DbTools.findByHql(hql);
		if(list.size()>0){
			JSONArray array = new JSONArray();
			for(int i = 0;i<list.size();i++){
				Wdwzforryd ryd = (Wdwzforryd) list.get(i);
				array.add(ryd);
			}
			ServletActionContext.getResponse().getOutputStream().write(array.toString().getBytes("utf-8"));
		}
	}
	
	public void getZhdByKeyword() throws UnsupportedEncodingException, IOException{
		HttpServletRequest request = ServletActionContext.getRequest();
		String keyword = request.getParameter("keyword");
		String hql = "from Jbqk01 where jbqk0110 like '%"+keyword+"%'";
		List list = DbTools.findByHql(hql);
		if(list.size()>0){
			JSONArray array = new JSONArray();
			for(int i = 0;i<list.size();i++){
				Jbqk01 jbqk = (Jbqk01) list.get(i);
				String id= jbqk.getId().getJbqk0100();
				String name = jbqk.getJbqk0110();
				String biaoshi = jbqk.getId().getJbqk0102();
				JSONObject obj = new JSONObject();
				obj.accumulate("name", name);
				obj.accumulate("id",id);
				obj.accumulate("biaoshi", biaoshi);
				array.add(obj);
			}
			ServletActionContext.getResponse().getOutputStream().write(array.toString().getBytes("utf-8"));
		}
	}
	
	
	public void insertWdwzForZhdn() throws UnsupportedEncodingException, IOException{
		HttpServletRequest request = ServletActionContext.getRequest();
		String gd = request.getParameter("gd");
		String jd = request.getParameter("jd");
		String wd = request.getParameter("wd");
		String zhdbm = request.getParameter("zhdbm");
		String dwsj = request.getParameter("dwsj");
		String biaoshi = request.getParameter("biaoshi");
		int year = Integer.valueOf(dwsj.split("年")[0]);
		int month = Integer.valueOf(dwsj.split("年")[1].split("月")[0]);
		int day = Integer.valueOf(dwsj.split("年")[1].split("月")[1].split("日")[0]);
		int hrs = Integer.valueOf(dwsj.split("年")[1].split("月")[1].split("日")[1].split("时")[0]);
		int min = Integer.valueOf(dwsj.split("年")[1].split("月")[1].split("日")[1].split("时")[1].split("分")[0]);
		int sec = Integer.valueOf(dwsj.split("年")[1].split("月")[1].split("日")[1].split("时")[1].split("分")[1].split("秒")[0]);
		Date date = new Date(year-1900, month-1, day, hrs, min, sec);
		Wdwzforzhdn zhdn = new Wdwzforzhdn();
		zhdn.setDwsj(date);
		zhdn.setGd(gd);
		zhdn.setJd(jd);
		zhdn.setWd(wd);
		zhdn.setZhdbm(zhdbm);
		zhdn.setJbqk0102(biaoshi);
		if(DbTools.saveObj(zhdn)){
			ServletActionContext.getResponse().getOutputStream().write("true".getBytes("utf-8"));
		}else{
			ServletActionContext.getResponse().getOutputStream().write("false".getBytes("utf-8"));
		}
	} 
	
	public void insertWdwzForRyd() throws UnsupportedEncodingException, IOException{
		HttpServletRequest request = ServletActionContext.getRequest();
		String gd = request.getParameter("gd");
		String jd = request.getParameter("jd");
		String wd = request.getParameter("wd");
		String dwsj = request.getParameter("dwsj");
		String mc = request.getParameter("mc");
		int year = Integer.valueOf(dwsj.split("年")[0]);
		int month = Integer.valueOf(dwsj.split("年")[1].split("月")[0]);
		int day = Integer.valueOf(dwsj.split("年")[1].split("月")[1].split("日")[0]);
		int hrs = Integer.valueOf(dwsj.split("年")[1].split("月")[1].split("日")[1].split("时")[0]);
		int min = Integer.valueOf(dwsj.split("年")[1].split("月")[1].split("日")[1].split("时")[1].split("分")[0]);
		int sec = Integer.valueOf(dwsj.split("年")[1].split("月")[1].split("日")[1].split("时")[1].split("分")[1].split("秒")[0]);
		Date date = new Date(year-1900, month-1, day, hrs, min, sec);
		Wdwzforryd ryd = new Wdwzforryd();
		ryd.setGd(gd);
		ryd.setJd(jd);
		ryd.setWd(wd);
		ryd.setDwsj(date);
		ryd.setMc(mc);
		if(DbTools.saveObj(ryd)){
			ServletActionContext.getResponse().getOutputStream().write("true".getBytes("utf-8"));
		}else{
			ServletActionContext.getResponse().getOutputStream().write("false".getBytes("utf-8"));
		}
	}
	
	public void queryWdwzforzhdnByCondition() throws UnsupportedEncodingException, IOException{
		HttpServletRequest request = ServletActionContext.getRequest();
		String eTime = request.getParameter("eTime");
		String sTime =request.getParameter("sTime");
		String zhdbm = request.getParameter("zhdbm");
		String zhdBiaoshi = request.getParameter("zhdBiaoshi");
		String hql = "from Wdwzforzhdn where zhdbm = '"+zhdbm+"' and jbqk0102 = '"+zhdBiaoshi+"'";
		if(!"".equals(sTime)){
			hql = hql +" and dwsj >= '"+sTime+"'";
		}
		if(!"".equals(eTime)){
			hql = hql +" and dwsj <= '"+eTime+"'";
		}
		List list = DbTools.findByHql(hql);
		if(list.size()>0){
			JSONArray array = new JSONArray();
			for(int i = 0;i<list.size();i++){
				Wdwzforzhdn zhd = (Wdwzforzhdn) list.get(i);
				array.add(zhd);
			}
			ServletActionContext.getResponse().getOutputStream().write(array.toString().getBytes("utf-8"));
		}
	}
	
	public void queryGpslineforzhdByCondition() throws UnsupportedEncodingException, IOException{
		HttpServletRequest request = ServletActionContext.getRequest();
		String eTime = request.getParameter("eTime");
		String sTime =request.getParameter("sTime");
		String zhdbm = request.getParameter("zhdbm");
		String zhdBiaoshi = request.getParameter("zhdBiaoshi");
		String hql = "from YjdcGpslineforzhd where zhdbh = '"+zhdbm+"' and jbqk0102 = '"+zhdBiaoshi+"'";
		if(!"".equals(sTime)){
			hql = hql +" and dwsj >= '"+sTime+"'";
		}
		if(!"".equals(eTime)){
			hql = hql +" and dwsj <= '"+eTime+"'";
		}
		List list = DbTools.findByHql(hql);
		if(list.size()>0){
			JSONArray array = new JSONArray();
			for(int i = 0;i<list.size();i++){
				YjdcGpslineforzhd zhd = (YjdcGpslineforzhd) list.get(i);
				array.add(zhd);
			}
			ServletActionContext.getResponse().getOutputStream().write(array.toString().getBytes("utf-8"));
		}
	}
	
	
	
}
